﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using MengRan.Models;
using MengRan.Models.index;
using MengRan.Models.test;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MengRan.Controllers.api
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class liuyanController : Controller
    {
        private readonly MyDbContext _dbContext;
        public liuyanController(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        [HttpGet]
        public IActionResult liuyan()
        {
            string ip = "http://124.222.78.73:8080/api/people/getall";


            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ip);
            WebResponse resp = req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.Default);
            string sReturn = sr.ReadToEnd().Trim();

            //JObject result = JObject.Parse(sReturn);
            //string message = result.Value<JToken>().ToString();


            //    string data = result.ToString();
            //    var datas = "[" + data.TrimStart("{".ToCharArray()).TrimEnd("}".ToCharArray()) + "]";
            List<liuyanmes> jsonq = JsonConvert.DeserializeObject<List<liuyanmes>>(sReturn);

            return Ok(new { code = 0, count = jsonq.Count, data = jsonq, mes = "" });

        }
        [HttpGet]
        public IActionResult Zeng(string name, string pad)
        {
            var a = "";
            using (_dbContext)
            {
                var us = new userr
                {
                    username = name,
                    password = pad,
                    settime = DateTime.Now
                };
                _dbContext.use.Add(us);
                var i = _dbContext.SaveChanges();
                a = i > 0 ? "数据写入成功" : "数据写入失败";
            }
            return Ok(a);
        }
       
        [HttpGet]
        public IActionResult setall()
        {
            using (_dbContext)
            {
                var list = _dbContext.use.ToList();
                var a = list.Count;
                return Ok(new { code = 0, count = a, data = list, mes = "" });
            }
        }
        [HttpGet]
        public IActionResult layui()
        {
            string ip = "https://layui.itze.cn//test/table/demo1.json";


            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ip);
            WebResponse resp = req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.Default);
            string sReturn = sr.ReadToEnd().Trim();

            JObject result = JObject.Parse(sReturn);
            string message = result.Value<JToken>("data").ToString();


            //    string data = result.ToString();
            //    var datas = "[" + data.TrimStart("{".ToCharArray()).TrimEnd("}".ToCharArray()) + "]";
            List<layui> jsonq = JsonConvert.DeserializeObject<List<layui>>(message);

            return Ok(new { code = 0, count = jsonq.Count, data = jsonq, mes = "" });

        }
        //[HttpGet]
        //public IActionResult chatmes()
        //{

        //    string ip = "http://124.222.78.73:8082/api/chat/getall";

        //    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ip);
        //    WebResponse resp = req.GetResponse();
        //    StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.Default);
        //    string sReturn = sr.ReadToEnd().Trim();
        //    List<Models.index.chatmes> jsonq = JsonConvert.DeserializeObject<List<Models.index.chatmes>>(sReturn);
        //    return Ok(new { code = 0, count = jsonq.Count, data = jsonq, mes = "" });
        //}
        [HttpGet]
        public IActionResult chatmes()
        {

            using (_dbContext)
            {
                var list = _dbContext.chatmess.OrderByDescending(b => b.id).ToList();
                return Ok(new { code = 0, count = list.Count, data = list, mes = "" });
            }
        }


        [HttpGet]
        public IActionResult delete()
        {

            return Ok();
        }
        [HttpGet]
        public string Create(string ip, string cid, string ipweizhi, string browser)
        {
            var message = "";
            using (_dbContext)
            {
                var a = new login
                {
                    ip = ip,
                    cid = cid,
                    ipweizhi = ipweizhi,
                    browser = browser,
                    logintime = DateTime.Now
                };
                _dbContext.logins.Add(a);
                var i = _dbContext.SaveChanges();
                message = i > 0 ? "添加成功" : "添加失败";
            }
            return message;
        }
        [HttpGet]
        public IActionResult GetAll()
        {
            using (_dbContext)
            {
                var list = _dbContext.logins.OrderByDescending(b => b.logintime).ToList();
                var a = list.Count;
                return Ok(new { code = 0, count = a, data = list, mes = "" });
            }
        }
       
    }
}
